package com.ldj.struts.action;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.ldj.common.SystemSession;
import com.ldj.hibernate.BackPayDAO;
import com.ldj.hibernate.ViewBackPay;
import com.ldj.hibernate.ViewBackPayDAO;
import com.ldj.hibernate.ViewBackPayId;
import com.ldj.hibernate.ViewContractDAO;
import com.ldj.hibernate.ViewContractId;

public class BackPayStatisticsAction extends DispatchAction {

	/**
	 * 工资监控报表
	 */
	
	public ActionForward find(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String year=new SimpleDateFormat("yyyy").format(Calendar.getInstance().getTime());
		int endYear = Integer.parseInt(year); 
		request.setAttribute("endYear", endYear);
		return mapping.findForward("find");
	}
	
	public ActionForward detail(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String syear = request.getParameter("syear");
		String smonth = request.getParameter("smonth");
		if(syear != null && !"".equals(syear))
			syear = syear + "-" + smonth;
		String eyear = request.getParameter("eyear");
		String emonth = request.getParameter("emonth");
		if(eyear != null && !"".equals(eyear))
			eyear = eyear + "-" + emonth;
		String areaCode = request.getParameter("areaCode");
		ViewBackPayDAO viewBackPayDao = new ViewBackPayDAO(request, response);
		List backPaylist = viewBackPayDao.find(areaCode, syear, eyear);
		request.setAttribute("list", backPaylist);
		return mapping.findForward("detail");
	}
	
	public ActionForward xls(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String syear = request.getParameter("syear");
		String smonth = request.getParameter("smonth");
		if(syear != null && !"".equals(syear))
			syear = syear + "-" + smonth;
		String eyear = request.getParameter("eyear");
		String emonth = request.getParameter("emonth");
		if(eyear != null && !"".equals(eyear))
			eyear = eyear + "-" + emonth;
		String areaCode = request.getParameter("areaCode");
		ViewBackPayDAO viewBackPayDao = new ViewBackPayDAO(request, response);
		List backPaylist = viewBackPayDao.find(areaCode, syear, eyear);
		request.setAttribute("list", backPaylist);
		return mapping.findForward("xls");
	}
	
	public ActionForward pic(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String syear = request.getParameter("syear");
		String smonth = request.getParameter("smonth");
		if(syear != null && !"".equals(syear))
			syear = syear + "-" + smonth;
		String eyear = request.getParameter("eyear");
		String emonth = request.getParameter("emonth");
		if(eyear != null && !"".equals(eyear))
			eyear = eyear + "-" + emonth;
		String areaCode = request.getParameter("areaCode");
		ViewBackPayDAO viewBackPayDao = new ViewBackPayDAO(request, response);
		List backPaylist = viewBackPayDao.find(areaCode, syear, eyear);
		Hashtable rd = new Hashtable();
		rd.put("barTitle", "各类型企业欠薪情况柱状图");
		rd.put("domain", "企业类型");
		rd.put("range", "企业数量");
		List dList = new Vector();
		if(backPaylist != null && backPaylist.size() > 0){
			Iterator iterator =	backPaylist.iterator();
			while(iterator.hasNext()){
				Object[] ob = (Object[])iterator.next();
				Hashtable hd = new Hashtable();
				hd.put("name", ob[1]);
				hd.put("title", "企业总数量");
				hd.put("value", Integer.parseInt(ob[5].toString()));
				dList.add(hd);
				hd = new Hashtable();
				hd.put("name", ob[1]);
				hd.put("title", "欠薪企业数量");
				hd.put("value", Integer.parseInt(ob[2].toString()));
				dList.add(hd);
			}	
		}		
		rd.put("data", dList);
		request.getSession().setAttribute("data", rd);
		return mapping.findForward("pic");
	}
	
	public ActionForward piePic(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String syear = request.getParameter("syear");
		String smonth = request.getParameter("smonth");
		if(syear != null && !"".equals(syear))
			syear = syear + "-" + smonth;
		String eyear = request.getParameter("eyear");
		String emonth = request.getParameter("emonth");
		if(eyear != null && !"".equals(eyear))
			eyear = eyear + "-" + emonth;
		String areaCode = request.getParameter("areaCode");
		ViewBackPayDAO viewBackPayDao = new ViewBackPayDAO(request, response);
		List backPaylist = viewBackPayDao.find(areaCode, syear, eyear);
		Hashtable rd = new Hashtable();
		rd.put("pieTitle", "各类型企业欠薪比例图");
		List dList = new Vector();
		if(backPaylist != null && backPaylist.size() > 0){
			Iterator iterator =	backPaylist.iterator();
			while(iterator.hasNext()){
				Object[] ob = (Object[])iterator.next();
				Hashtable hd = new Hashtable();
				hd.put("name",ob[1] );
				hd.put("value",Integer.parseInt(ob[2].toString()));
				dList.add(hd);
			}
		}		
		rd.put("data", dList);
		request.getSession().setAttribute("data", rd);
		return mapping.findForward("piePic");
	}
	
	
	public ActionForward unitFind(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String year=new SimpleDateFormat("yyyy").format(Calendar.getInstance().getTime());
		int endYear = Integer.parseInt(year); 
		request.setAttribute("endYear", endYear);
		return mapping.findForward("unitFind");
	}
	public ActionForward unitDetail(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String syear = request.getParameter("syear");
		String smonth = request.getParameter("smonth");
		if(syear != null && !"".equals(syear))
			syear = syear + "-" + smonth;
		String eyear = request.getParameter("eyear");
		String emonth = request.getParameter("emonth");
		if(eyear != null && !"".equals(eyear))
			eyear = eyear + "-" + emonth;
		String areaCode = request.getParameter("areaCode");
		ViewBackPayDAO viewBackPayDao = new ViewBackPayDAO(request, response);
		List backPaylist = viewBackPayDao.unitFind(areaCode, syear, eyear);
		request.setAttribute("list", backPaylist);
		return mapping.findForward("unitDetail");
	}
	
	public ActionForward unitXls(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String syear = request.getParameter("syear");
		String smonth = request.getParameter("smonth");
		if(syear != null && !"".equals(syear))
			syear = syear + "-" + smonth;
		String eyear = request.getParameter("eyear");
		String emonth = request.getParameter("emonth");
		if(eyear != null && !"".equals(eyear))
			eyear = eyear + "-" + emonth;
		String areaCode = request.getParameter("areaCode");
		ViewBackPayDAO viewBackPayDao = new ViewBackPayDAO(request, response);
		List backPaylist = viewBackPayDao.unitFind(areaCode, syear, eyear);
		request.setAttribute("list", backPaylist);
		return mapping.findForward("unitXls");
	}
	
	public ActionForward oganFind(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String year=new SimpleDateFormat("yyyy").format(Calendar.getInstance().getTime());
		int endYear = Integer.parseInt(year); 
		request.setAttribute("endYear", endYear);
		return mapping.findForward("oganFind");
	}
	
	public ActionForward oganDetail(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String syear = request.getParameter("syear");
		String smonth = request.getParameter("smonth");
		if(syear != null && !"".equals(syear))
			syear = syear + "-" + smonth;
		String eyear = request.getParameter("eyear");
		String emonth = request.getParameter("emonth");
		if(eyear != null && !"".equals(eyear))
			eyear = eyear + "-" + emonth;
		String areaCode = request.getParameter("areaCode");
		ViewBackPayDAO viewBackPayDao = new ViewBackPayDAO(request, response);
		List backPaylist = viewBackPayDao.oganFind(areaCode, syear, eyear);
		request.setAttribute("list", backPaylist);
		return mapping.findForward("oganDetail");
	}
	
	public ActionForward oganXls(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String syear = request.getParameter("syear");
		String smonth = request.getParameter("smonth");
		if(syear != null && !"".equals(syear))
			syear = syear + "-" + smonth;
		String eyear = request.getParameter("eyear");
		String emonth = request.getParameter("emonth");
		if(eyear != null && !"".equals(eyear))
			eyear = eyear + "-" + emonth;
		String areaCode = request.getParameter("areaCode");
		ViewBackPayDAO viewBackPayDao = new ViewBackPayDAO(request, response);
		List backPaylist = viewBackPayDao.oganFind(areaCode, syear, eyear);
		request.setAttribute("list", backPaylist);
		return mapping.findForward("oganXls");
	}
	
	public ActionForward oganPic(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String syear = request.getParameter("syear");
		String smonth = request.getParameter("smonth");
		if(syear != null && !"".equals(syear))
			syear = syear + "-" + smonth;
		String eyear = request.getParameter("eyear");
		String emonth = request.getParameter("emonth");
		if(eyear != null && !"".equals(eyear))
			eyear = eyear + "-" + emonth;
		String areaCode = request.getParameter("areaCode");
		ViewBackPayDAO viewBackPayDao = new ViewBackPayDAO(request, response);
		List backPaylist = viewBackPayDao.oganFind(areaCode, syear, eyear);
		Hashtable rd = new Hashtable();
		rd.put("barTitle", "各区域企业欠薪情况柱状图");
		rd.put("domain", "区域");
		rd.put("range", "企业数量");
		List dList = new Vector();
		if(backPaylist != null && backPaylist.size() > 0){
			Iterator iterator =	backPaylist.iterator();
			while(iterator.hasNext()){
				Object[] ob = (Object[])iterator.next();
				Hashtable hd = new Hashtable();
				hd.put("name", ob[1]);
				hd.put("title", "企业总数量");
				hd.put("value", ob[5]);
				dList.add(hd);
				hd = new Hashtable();
				hd.put("name", ob[1]);
				hd.put("title", "欠薪企业数量");
				hd.put("value", ob[2]);
				dList.add(hd);
			}	
		}		
		rd.put("data", dList);
		request.getSession().setAttribute("data", rd);
		return mapping.findForward("oganPic");
	}
	
	public ActionForward oganPiePic(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String syear = request.getParameter("syear");
		String smonth = request.getParameter("smonth");
		if(syear != null && !"".equals(syear))
			syear = syear + "-" + smonth;
		String eyear = request.getParameter("eyear");
		String emonth = request.getParameter("emonth");
		if(eyear != null && !"".equals(eyear))
			eyear = eyear + "-" + emonth;
		String areaCode = request.getParameter("areaCode");
		ViewBackPayDAO viewBackPayDao = new ViewBackPayDAO(request, response);
		List backPaylist = viewBackPayDao.oganFind(areaCode, syear, eyear);
		Hashtable rd = new Hashtable();
		rd.put("pieTitle", "各区域企业欠薪比例图");
		List dList = new Vector();
		if(backPaylist != null && backPaylist.size() > 0){
			Iterator iterator =	backPaylist.iterator();
			while(iterator.hasNext()){
				Object[] ob = (Object[])iterator.next();
				Hashtable hd = new Hashtable();
				hd.put("name",ob[1] );
				hd.put("value",ob[2]);
				dList.add(hd);
			}
		}		
		rd.put("data", dList);
		request.getSession().setAttribute("data", rd);
		return mapping.findForward("oganPiePic");
	}
	
}